var name = /^[\u4e00-\u9fa5]{2,4}$/; //2-4个中文字符正则
var phone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
var aaa = /^(0\d{2,3})-?(\d{7,8})$/; //座机号码
var aaaa = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; //电子邮箱
var money = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/ //金额两位小数
var password1 = /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{6,20}$/; //密码必须由字母、数字、特殊符号组成，区分大小写
var password2 = /^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,20}$/; //密码必须由字母、数字组成，区分大小写
var password3 = /^[a-z0-9_-]{8,20}$/; //密码由字母、数字组成，区分大小写

/**
 * 验证用户名
 */
const zzname = (value) => {
	if (value == "") {
		uni.showToast({
			icon: 'none',
			title: "用户名不能为空！",
			duration: 2000
		});
		return false;
	} else if (!name.test(value)) {
		uni.showToast({
			icon: 'none',
			title: "用户名只能为2-4中文！",
			duration: 2000
		});
		return false;
	} else {
		return true;
	}

}
/**
 * 验证手机号
 */
const zzphone = (value) => {
	if (value == "") {
		uni.showToast({
			icon: 'none',
			title: "请填写您的手机号码！",
			duration: 2000
		});
		return false;
	} else if (!phone.test(value)) {
		uni.showToast({
			icon: 'none',
			title: "请正确填写您的手机号码！",
			duration: 2000
		});
		return false;
	} else {
		return true;
	}


}
/**
 * 验证密码
 */
const zzpassword = (value) => {
	if (value == "") {
		uni.showToast({
			icon: 'none',
			title: "请输入密码！",
			duration: 2000
		});
		return false;
	} else if (!password3.test(value)) {
		uni.showToast({
			icon: 'none',
			title: "请正确填写您的密码！",
			duration: 2000
		});
		return false;
	} else {
		return true;
	}


}
/**
 * 验证小数点后两位及多个小数
 */
const zzmoney = (value) => {
	if (value == "") {
		uni.showToast({
			icon: 'none',
			title: "请输入金额！",
			duration: 2000
		});
		return false;
	} else if (!money.test(value)) {
		uni.showToast({
			icon: 'none',
			title: "请输入正确金额！",
			duration: 2000
		});
		return false;
	} else {
		return true;
	}


}

/**
 * 时间戳转换
 * @param time 时间戳
 * @returns {Function} 2000-00-00 00:00:00
 * @constructor
 */
const times = (time) => {
	var now = new Date(time);
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds();
	if (minute < 10) {
		minute = '0' + minute
	}
	if (second < 10) {
		second = '0' + second
	}
	if (hour < 10) {
		hour = '0' + hour
	}
	if (month < 10) {
		month = '0' + month
	}
	if (date < 10) {
		date = '0' + date
	}
	return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
};

/**
 * 时间戳转换
 * @param time 时间戳
 * @returns {Function} 2000 00 00
 * @constructor
 */
const timesa = (time) => {
	var now = new Date(time);
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();

	if (month < 10) {
		month = '0' + month
	}
	if (date < 10) {
		date = '0' + date
	}
	return year.toString() + month.toString() + date.toString();
};

/**
 * 获取日期的今天，明天，后天
 * @param value 开始计算的时间戳 默认值当天
 * @param addDayCount -1是昨天，0是今天，1是后一天
 * @returns {Function} 2000 00 00
 */
function getDateStr(value, addDayCount) {
	let date
	if (value) {
		date = new Date(value);
	} else {
		date = new Date();
	}
	date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期 
	let y = date.getFullYear();
	let m = date.getMonth() + 1; //获取当前月份的日期 
	let d = date.getDate();
	if (m < 10) {
		m = '0' + m;
	};
	if (d < 10) {
		d = '0' + d;
	};
	// console.log(y + "-" + m + "-" + d)
	return y + "-" + m + "-" + d;
}


/**
 * 获取今天起后几天
 * @param value 开始计算的时间戳 默认值当天
 * @param index 往后推进几天
 * @param type1  是否需要转化今天天明天后天  true / false
 * @param type2  是否需要添加周  true / false
 * @returns [ '01/01 周一' ,'01/02 周二' ] 
 */
function datetime(value, index, type1 = false, type2 = false) {
	let timestamp
	if (value) {
		timestamp = new Date.parse(value);
	} else {
		timestamp = Date.parse(new Date());
	}
	let tempDate = new Date(timestamp)
	let days = ''
	let week = ''
	let yue = ''
	let ri = ''
	let date = ''
	for (let i = 1; i < (index + 1); i++) {
		days = tempDate.getDay();
		yue = new Date(tempDate).getMonth() + 1 //=>0
		ri = new Date(tempDate).getDate() //=>6
		switch (days) {
			case 1:
				week = ' (周一)';
				break;
			case 2:
				week = ' (周二)';
				break;
			case 3:
				week = ' (周三)';
				break;
			case 4:
				week = ' (周四)';
				break;
			case 5:
				week = ' (周五)';
				break;
			case 6:
				week = ' (周六)';
				break;
			case 0:
				week = ' (周日)';
				break;
		}
		if (type1) {
			let jmh = i == 1 ? '今天' : i == 2 ? '明天' : i == 3 ? '后天' : yue + '/' + ri
		} else {
			let jmh = yue + '/' + ri
		}
		if (type2) {
			date.push(jmh + week)
		} else {
			date.push(jmh)
		}

		tempDate = new Date(timestamp + (86400000 * i))
	}
	return date
}

/**
 * 函数防抖 (只执行最后一次点击)
 * @param Function 要执行的函数
 * @param delay 延迟时间
 * @returns {Function}
 * @constructor
 */
export const Debounce = (fn, t) => {
	const delay = t || 500
	let timer
	return function() {
		const args = arguments
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			timer = null
			fn.apply(this, args)
		}, delay)
	}
}
/**
 * 函数节流
 * @param Function 要执行的函数
 * @param interval 间隔时间
 * @returns {Function}
 * @constructor
 */
export const Throttle = (fn, t) => {
	let last
	let timer
	const interval = t || 500
	return function() {
		const args = arguments
		const now = +new Date()
		if (last && now - last < interval) {
			clearTimeout(timer)
			timer = setTimeout(() => {
				last = now
				fn.apply(this, args)
			}, interval)
		} else {
			last = now
			fn.apply(this, args)
		}
	}
}

module.exports = {
	times,
	timesa,
	getDateStr,
	datetime,
	zzname,
	zzphone,
	zzpassword,
	Debounce,
	Throttle,
}